// baseUtils.js
function getCurrentTime() {
    const d = new Date();
    const year = d.getFullYear();
    const month = (d.getMonth() + 1).toString().padStart(2, '0');
    const day = d.getDate().toString().padStart(2, '0');
    const hours = d.getHours().toString().padStart(2, '0');
    const minutes = d.getMinutes().toString().padStart(2, '0');
    const seconds = d.getSeconds().toString().padStart(2, '0');
    let current_time = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    return current_time;
}

function truncateText(text, maxLength) {
    const result = text.length > maxLength ? text.slice(0, maxLength) + '...' : text;
    return result;
}

function throttle(fn, limit) {
    let lastFn;
    let lastRan;
    return function (...args) {
        if (!lastRan) {
            fn.apply(this, args);
            lastRan = Date.now();
        } else {
            clearTimeout(lastFn);
            lastFn = setTimeout(() => {
                if (Date.now() - lastRan >= limit) {
                    fn.apply(this, args);
                    lastRan = Date.now();
                }
            }, limit - (Date.now() - lastRan));
        }
    };
}

function generateTimestampBasedID() {
    const timestamp = Date.now().toString(36);
    const randomSuffix = Math.random().toString(36).substring(2, 6);
    return `${timestamp}${randomSuffix}`;
}

export { getCurrentTime, truncateText, throttle, generateTimestampBasedID };
